$(function(){
	var keyword = getParam("keyword")||'';//获取上一个页面输入的关键字
	var com = getParam("com")||'';
	$("#singleDomain").val(keyword);
	//通过录入的关键字查询出相关域名信息列表
	getDomainSetting(com);
	  /* 热门问题切换 */
    var head = document.getElementById("tab-head").getElementsByTagName("a");
    var body = document.getElementById("tab-body").getElementsByTagName("section");
    for (var a = 0; a < head.length; a++) {
        head[a].index = a;
        head[a].onclick = function () {
            for (var j = 0; j < body.length; j++) {
                body[j].style.display = "none";
                head[j].className = "";
            }
            body[this.index].style.display = "block";
            head[this.index].className = "active";
        }
    }
    /* 英文域名tab切换 */
    $("input[name='multiSufCatTag']").each(function () {
        $(this).click(function () {
            var _id = $(this).attr('id');
            $("#all_check").removeAttr("checked");
            if (_id == "en_radio") {
                $('#enDomain').show();
                $('#zhDomain').hide();
            }
            if (_id == "cn_radio") {
                $('#enDomain').hide();
                $('#zhDomain').show();
            }
        });
    });
    /*获取域名查询结果并展示*/
    checkDomain();
    getRecDomain();
    $("#checkSingle").click(function(){
    	 checkDomain();
    	 getRecDomain();
    });
    //域名推荐换一批
    $("a.random").on("click",function(){
    	getRecDomain();
    });
});
//获取推荐域名
function getRecDomain(){
	var keyword = $("#singleDomain").val();
	$("#recDomain").html("");
	if(!keyword){
		return;
	}
	$.ajax({
    	url: "/domain/getRecDomain.do",
    	data: {"domainName":keyword,"num":6},
    	dataType: "json",
    	success: function (data) {
    		$.each(data,function(i,n){
    			var rli='<li class="fl recDomain" style="width: 50%;">';
    		    rli += '<div class="fl">';
    		    rli += '<span class="domainName">'+n.substr(0,n.indexOf("."))+'</span><b class="suffix">'+n.substr(n.indexOf("."))+'</b>';
    		    rli += '<span class="status ">(未注册)</span>';
    		    rli += '</div>';
    		    rli += '<div class="fr">';
    		    rli += '<a href="/domain/whoisResult.do?domainName='+data[i]+'" class="add-list-btn" target="_blank">加入清单</a>';
    		    rli += '</div>';
    		    rli += '</li>';
    		    $("#recDomain").append(rli);
    		});
    	}
    });
}
//域名检索
function checkDomain(){
	$(".rs-list").html("");
	var suf = $("#suffix").val();
    var sufs = $("#suffix option").map(function(){return $(this).val();}).get();
    var index = sufs.indexOf(suf);
    sufs.splice(index,1);
    sufs.unshift(suf);
    var singleDomain =$("#singleDomain").val();
    if(!singleDomain){
    	return;
    }
    $.each(sufs,function(i,n){
    	sufs[i]=singleDomain+n;
    });
    $.each(sufs,function(i,n){
    	var prefix = n.substr(0,n.indexOf("."));
    	var suffix = n.substr(n.indexOf("."));
    	$.ajax({
        	url: "/domain/checkDomain.do",
        	data: "domainName="+n,
        	dataType: "json",
        	success: function (data) {
        		if(data){
        			if(data.infoDomain){
        				var data = JSON.parse(data.infoDomain);
	        			var li="<li class='rs-item'>";
	        			li += "<div class='fl'>";
	        			li += "<span class='domainName'>"+prefix+"</span>";
	        			li += "<b class='suffix'>"+suffix+"</b>";
	        			if(data.result){
	        				li += "<span class='status status-ok'>(未注册)</span>";
	        				li += "</div>";
	            			li += "<div class='fr'>";
	            			li += "<a href='' class='add-list-btn' target='_blank'>加入清单</a>";
	        			}
	        			else{
	        				li += "<span class='status'>(已注册)</span>";
	        				li += "</div>";
	            			li += "<div class='fr'>";
	            			li += "<a href='' class='add-list-btn' target='_blank'>查看域名信息</a>";
	        			}
	        			li += "</div>";
	        			li += "</li>";
	        			$(".rs-list").append(li);
        			}
        			else{
        				var li="<li class='rs-item'>";
	        			li += "<div class='fl'>";
	        			li += "<span class='domainName'>"+prefix+"</span>";
	        			li += "<b class='suffix'>"+suffix+"</b>";
	        			li += "<span class='status'></span>";
	        			li += "</div>";
	            		li += "<div class='fr' style='color:red;'>"+data.msg+"</div>";
	        			li += "</li>";
	        			$(".rs-list").append(li);
        			}
        		}
        	}
        });
    });
}

//多选模式，域名全选按钮
function selectAll(e) {
    var checkLanguage = $("input[name='multiSufCatTag']:checked").attr('id');
    var checklist = "";
    if (checkLanguage == "en_radio") {
        checklist = $("#enDomain").find('input[name="multiSufName"]');
    } else {
        checklist = $("#zhDomain").find('input[name="multiSufName"]');
    }
    if (e.checked) {
        for (var i = 0; i < checklist.length; i++) {
            console.log(1);
            checklist[i].checked = 1;
        }
    } else {
        for (var j = 0; j < checklist.length; j++) {
            console.log(2);
            checklist[j].checked = 0;
        }
    }
}
//通过录入的关键字查询出相关域名信息列表
function getDomainSetting(com){
	$.ajax({
    	url: "/setting/queryDomainSetting.do",
    	dataType: "json",
    	async: false,
    	success: function (data) {
    		if(data){
    			//域名后缀加载
    			if(data.suffix){
    				var suffixs = data.suffix.split(";");
    				var option = "";
    				for (var index in suffixs) {
    					var suffix =suffixs[index];
    					if(suffix){
    						if(com==suffix){
    							option += '<option value=.'+suffix+' selected>.'+suffix+'</option>';
    						}else{
    							option += '<option value=.'+suffix+'>.'+suffix+'</option>';
    						}
    					}
    				}
    				$("#suffix").html(option);
    			}
    			//域名小知识加载
    			if(data.domainKnowledge){
    				var domainKnowledges =data.domainKnowledge;
    				var li="";
    				for (var i = 0; i < domainKnowledges.length; i++) {
    					var domainKnowledge = domainKnowledges[i];
    					li+="<li><a href=''>▪ "+domainKnowledge.name+"</a></li>"; 
    				}
    				$("#domainKnowledge").html(li);
    			}
    			//域名查询-下方宣传
    			if(data.bottomBanner){
    				var bottomBanner = data.bottomBanner||'';
    				$("#bottomBanner").html(bottomBanner);
    			}
    			//获取英文域名后缀
    			if(data.suffixEn){
    				var suffixEns = data.suffixEn.split(",");
    				var labelEn = '';
    				for (var i = 0; i < suffixEns.length; i++) {
    					var suffixEn = suffixEns[i];
    					labelEn+="<label><input type='checkbox' name='multiSufName' value="+suffixEn+">"+suffixEn+"</label>";
    				}
    				$("#enDomain").html(labelEn);
    			}
    			//获取中文域名后缀
    			if(data.suffixZh){
    				var suffixZhs = data.suffixZh.split(",");
    				var labelZh = '';
    				for (var i = 0; i < suffixZhs.length; i++) {
    					var suffixZh = suffixZhs[i];
    					labelZh+="<label><input type='checkbox' name='multiSufName' value="+suffixZh+">"+suffixZh+"</label>";
    				}
    				$("#zhDomain").html(labelZh);
    			}
    		}
    	}
    });
}
